package com.amigo.online.provider.video.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.result.VideoInfo;
import com.amigo.online.provider.video.model.entity.VideoRecomend;

@Mapper
@Component
public interface VideoRecommendMapper extends MyMapper<VideoRecomend> {

    @Select("SELECT v.id ,v.music_id ,v.video,v.cover,v.duration ,v.href,v.type,v.ads,v.user_id, " +
            "v.recommended,v.longitude,v.latitude,v.up,v.look,v.comment ,v.destroy,v.content," +
            "v.title,v.created_time as createdTime,v.updated_time,v.deleted ,v.deleted_time,v.width,v.height,v.share," +
            "v.location_id as locationId,v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted,m.name, m.music, m.id as music_id," +
            "m.type,m.cover as music_cover,m.user_id as m_user_id,m.category,m.recommended as m_recommended,m.author," +
            "m.video_id,m.used,m.created_time as m_created_time,m.duration as m_duration, u.nickname,u.avatar,u.avatar_small,u.account " +
            " FROM" +
            "	video_tbl v " +
            "LEFT JOIN music_tbl m ON v.music_id = m.id " +
            "LEFT JOIN user_tbl u ON v.user_id = u.id " +
            "WHERE " +
            "	v.deleted = 0 " +
            "AND u.`status` = 1 " +
            "AND v.created_time > unix_timestamp() - 90 * 24 * 60 * 60 " +
            "ORDER BY " +
            "	v.weight DESC " +
            "LIMIT 0,10")
    @Results(id = "videoInfoResult", value = {
            @Result(column = "type", jdbcType = JdbcType.INTEGER),
            @Result(column = "ads", property = "ads", jdbcType = JdbcType.INTEGER),
            @Result(column = "recommended", jdbcType = JdbcType.INTEGER),
            @Result(column = "allowe_promoted", property = "allowe_promoted", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "m_recommended", jdbcType = JdbcType.INTEGER),
            @Result(column = "music_id", property = "music.id"),
            @Result(column = "name", property = "music.name"),
            @Result(column = "type", property = "music.type"),
            @Result(column = "music_cover", property = "music.cover"),
            @Result(column = "m_user_id", property = "music.userId"),
            @Result(column = "category", property = "music.category"),
            @Result(column = "m_recommended", property = "music.recommended"),
            @Result(column = "video_id", property = "music.videoId"),
            @Result(column = "used", property = "music.used"),
            @Result(column = "music", property = "music.music"),
            @Result(column = "m_duration", property = "music.duration"),
            @Result(column = "nickname", property = "user.nickname"),
            @Result(column = "avatar", property = "user.avatar"),
            @Result(column = "avatar_small", property = "user.avatar_small"),
            @Result(column = "account", property = "user.account"),
            @Result(column = "user_type", property = "user.type")
    })
    List<VideoInfo> getRecomendedVideo(int page, int size);

    @Select("<script>" +
            "SELECT v.id ,v.music_id ,v.video,v.cover,v.duration ,v.href,v.type,v.ads,v.user_id," +
            "v.recommended,v.longitude,v.latitude,v.up,v.look,v.comment ,v.destroy,v.content," +
            "v.title,v.created_time as createdTime,v.updated_time,v.deleted ,v.deleted_time,v.width,v.height,v.share," +
            "v.location_id as locationId,v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted ,m.name, m.music, m.id as music_id," +
            "m.type,m.cover as music_cover,m.user_id as m_user_id,m.category,m.recommended as m_recommended,m.author," +
            "m.video_id,m.used,m.created_time as m_created_time,m.duration as m_duration, u.nickname,u.avatar,u.avatar_small,u.account,u.type as user_type" +
            " FROM" +
            "	video_tbl v " +
            "LEFT JOIN music_tbl m ON v.music_id = m.id " +
            "LEFT JOIN user_tbl u ON v.user_id = u.id " +
            "WHERE " +
            "	v.deleted = 0 and v.ads = 0 and v.recommended = 1 " +
            "AND u.`status` = 1 " +
            "AND v.created_time > unix_timestamp() - 150 * 24 * 60 * 60 " +
            "<if test=\"videoIds != null and !videoIds.isEmpty() \">" +
            "    AND v.id not in\n" +
            "    <foreach collection=\"videoIds\" item=\"videoId\" open=\"(\" close=\")\" separator=\",\">" +
            "        #{videoId}" +
            "    </foreach>" +
            "</if>" +
            "ORDER BY " +
            "	v.weight DESC " +
            "LIMIT #{start},#{size}" +
            "</script>")
    @ResultMap("videoInfoResult")
    List<VideoInfo> getRecomendedVideoNotInIds(@Param("start") int start, @Param("size") int size, @Param("videoIds") List<Long> videoIds);
    @Select("<script>" +
            "SELECT v.id ,v.music_id ,v.video,v.cover,v.duration ,v.href,v.type,v.ads,v.user_id," +
            "v.recommended,v.longitude,v.latitude,v.up,v.look,v.comment ,v.destroy,v.content," +
            "v.title,v.created_time as createdTime,v.updated_time,v.deleted ,v.deleted_time,v.width,v.height,v.share," +
            "v.location_id as locationId,v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted ,m.name, m.music, m.id as music_id," +
            "m.type,m.cover as music_cover,m.user_id as m_user_id,m.category,m.recommended as m_recommended,m.author," +
            "m.video_id,m.used,m.created_time as m_created_time,m.duration as m_duration, u.nickname,u.avatar,u.avatar_small,u.account,u.type as user_type" +
            " FROM" +
            "	video_tbl v " +
            "LEFT JOIN music_tbl m ON v.music_id = m.id " +
            "LEFT JOIN user_tbl u ON v.user_id = u.id " +
            "WHERE " +
            "	v.deleted = 0 and v.ads = 0 and v.recommended = 1 " +
            "AND u.`status` = 1 " +
            "AND v.`type` = 3 " +
            "AND v.created_time > unix_timestamp() - 150 * 24 * 60 * 60 " +
            "<if test=\"videoIds != null and !videoIds.isEmpty() \">" +
            "    AND v.id not in\n" +
            "    <foreach collection=\"videoIds\" item=\"videoId\" open=\"(\" close=\")\" separator=\",\">" +
            "        #{videoId}" +
            "    </foreach>" +
            "</if>" +
            "ORDER BY " +
            "	v.weight DESC " +
            "LIMIT 0,#{size}" +
            "</script>")
    @ResultMap("videoInfoResult")
    List<VideoInfo> getModleVideoNotInIds( @Param("size") int size, @Param("videoIds") List<Long> videoIds);


            @Select("<script>" +
                    "SELECT v.id ,v.music_id ,v.video,v.cover,v.duration ,v.href,v.type,v.ads,v.user_id," +
                    "v.recommended,v.longitude,v.latitude,v.up,v.look,v.comment ,v.destroy,v.content," +
                    "v.title,v.created_time as createdTime,v.updated_time,v.deleted ,v.deleted_time,v.width,v.height,v.share," +
                    "v.location_id as locationId,v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted ,m.name, m.music, m.id as music_id," +
                    "m.type,m.cover as music_cover,m.user_id as m_user_id,m.category,m.recommended as m_recommended,m.author," +
                    "m.video_id,m.used,m.created_time as m_created_time,m.duration as m_duration, u.nickname,u.avatar,u.avatar_small,u.account,u.type as user_type" +
                    " FROM" +
                    "	video_tbl v " +
                    "LEFT JOIN music_tbl m ON v.music_id = m.id " +
                    "LEFT JOIN user_tbl u ON v.user_id = u.id " +
                    "WHERE " +
                    "<if test=\"videoIds != null and !videoIds.isEmpty() \">" +
                    "    v.id in\n" +
                    "    <foreach collection=\"videoIds\" item=\"videoId\" open=\"(\" close=\")\" separator=\",\">" +
                    "        #{videoId}" +
                    "    </foreach>" +
                    "</if>" +
                    "ORDER BY " +
            "	v.weight DESC " +
            "</script>")
    @ResultMap("videoInfoResult")
    List<VideoInfo> getRecomendedVideoInIds(@Param("videoIds") List<Long> videoIds);

    @Select("<script>" +
            "SELECT v.id ,v.music_id ,v.video,v.cover,v.duration ,v.href,v.type,v.ads,v.user_id," +
            "v.recommended,v.longitude,v.latitude,v.up,v.look,v.comment ,v.destroy,v.content," +
            "v.title,v.created_time as createdTime,v.updated_time,v.deleted ,v.deleted_time,v.width,v.height,v.share," +
            "v.location_id as locationId,v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted ,m.name, m.music, m.id as music_id," +
            "m.type,m.cover as music_cover,m.user_id as m_user_id,m.category,m.recommended as m_recommended,m.author," +
            "m.video_id,m.used,m.created_time as m_created_time,m.duration as m_duration, u.nickname,u.avatar,u.avatar_small,u.account,u.type as user_type" +
            " FROM" +
            "	video_tbl v " +
            "LEFT JOIN music_tbl m ON v.music_id = m.id " +
            "LEFT JOIN user_tbl u ON v.user_id = u.id " +
            "WHERE " +
            "	v.deleted = 0 and v.ads = 0 and v.look &lt; 100 and v.id > 39400 " +
            "AND u.`status` = 1 " +
            "AND v.created_time > unix_timestamp() - 90 * 24 * 60 * 60 " +
            "<if test=\"videoIds != null and !videoIds.isEmpty() \">" +
            "    AND v.id not in\n" +
            "    <foreach collection=\"videoIds\" item=\"videoId\" open=\"(\" close=\")\" separator=\",\">" +
            "        #{videoId}" +
            "    </foreach>" +
            "</if>" +
            "ORDER BY " +
            "	v.look DESC " +
            "LIMIT #{start},#{num}" +
            "</script>")
    @ResultMap("videoInfoResult")
    List<VideoInfo> getNewVideo(@Param("videoIds")List<Long> lookedVideoIds,@Param("start") Integer start, @Param("num") Integer num);

    @Select("select * from video_tbl where id = 33076")
    VideoInfo selectTemp();
}
